package team.first.dataimport;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.neo4j.driver.Session;
import team.first.util.DriverUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 然然然儿呐_
 * @Project_name: project
 * @Package_name: team.first
 * @Date: 2021/12/3 11:16
 * @Description:
 */
public class MovieImport {
    public static void movieImport() {
        Session session = DriverUtil.getNewSession();
        try {
            List<Entity> movieList = Db.use().query("select * from movie");

            String addMovie = "CREATE (:Movie{" +
                    "id:$id, " +
                    "originalTitle:$originalTitle," +
                    "chineseTitle:$chineseTitle," +
                    "aka:$aka," +
                    "year:$year," +
                    "region:$region," +
                    "duration:$duration," +
                    "language:$language," +
                    "pubDate:$pubDate," +
                    "summary:$summary," +
                    "posterUrl:$posterUrl," +
                    "rating:$rating," +
                    "poster:$poster" +
                    "})";
            movieList.forEach(movie -> {
                Map<String, Object> params = new HashMap<>();
                params.put("id", movie.getInt("id"));
                params.put("originalTitle", movie.getStr("original_title"));
                params.put("chineseTitle", movie.getStr("chinese_title"));
                params.put("aka", movie.getStr("aka"));
                params.put("year", movie.getInt("year"));
                params.put("region", movie.getStr("region"));
                params.put("duration", movie.getStr("duration"));
                params.put("language", movie.getStr("language"));
                params.put("pubDate", movie.getStr("pub_date"));
                params.put("summary", movie.getStr("summary"));
                params.put("posterUrl", movie.getStr("poster_url"));
                params.put("rating", movie.getDouble("rating"));
                params.put("poster", movie.getStr("poster"));
                session.run(addMovie, params);
            });
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DriverUtil.close(session);
        }

    }
}
